var app = getApp();
var config = require('../../libs/config.js');
wx.cloud.init({
  env: 'test-7gxzn7uuc171e9e9'
})
const DB = wx.cloud.database()
var that;
Page({
	data: {
		filterId: 1,
		address: '地址',
		markers: [],
    latitude: '',
    longitude: '',
    textData: {},
		banners: [
			{
				id: 1,
				img: 'https://img.51miz.com/Element/00/88/76/97/dc1bd534_E887697_eb728296.png',           //图片挂了，找几张
				url: '',
				name: '轮播图1'
			},
			{
				id: 2,
				img: 'https://tse4-mm.cn.bing.net/th/id/OIP.r1gxi9YCoHjv2_DTDofsrAHaGg?pid=Api&rs=1',
				url: '',
				name: '轮播图2'
			},
			{
				id: 3,
				img: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3757641683,3367667739&fm=15&gp=0.jpg',
				url: '',
				name: '轮播图3'
			}
		],
		shops: app.globalData.shops,
	},
	// getUserInfo: function(e) {
  //   let that = this;
  //   // console.log(e)
  //   // 获取用户信息
  //   wx.getSetting({
  //     success(res) {
  //       // console.log("res", res)
  //       if (res.authSetting['scope.userInfo']) {
  //         console.log("已授权=====")
  //         // 已经授权，可以直接调用 getUserInfo 获取头像昵称
  //         wx.getUserInfo({
  //           success(res) {
  //             console.log("获取用户信息成功", res)
  //             that.setData({
	// 							name: res.userInfo.nickName
								
  //             })
  //           },
  //           fail(res) {
  //             console.log("获取用户信息失败", res)
  //           }
  //         })
  //       } else {
  //         console.log("未授权=====")
  //         that.showSettingToast("请授权")
  //       }
	// 		}
	// 	})
	// },
	//   // 打开权限设置页提示框
	// 	showSettingToast: function(e) {
	// 		wx.showModal({
	// 			title: '提示！',
	// 			confirmText: '去设置',
	// 			showCancel: false,
	// 			content: e,
	// 			success: function(res) {
	// 				if (res.confirm) {
	// 					wx.navigateTo({
	// 						url: '../setting/setting',
	// 					})
	// 				}
	// 			}
	// 		})
	// 	},

	userAdd(openId) {
    var user = {
      openId: openId
    };
    wx.setStorageSync('user', user);
  },
onLoad: function() {
    wx.cloud.callFunction({
			// name: "deleteData",
      name: "getOpenid",
      success(res) {
        let openId = res.result.openid
        app.globalData.nowuseropid = app.globalData.openid
        // 判断数据库中是否已经有数据
        DB.collection('user').where({
          openId: openId,
        })
        .get().then(ress => {
          console.log('ressress',ress.data[0])
          
          if (ress.data.length == 0) {
          	DB.collection('user').add({
							data:{
								openId: openId,
							},
							success:res => {
								this.setData({
									openId: openId,
								})
								console.log('上传成功', res)
							}
							})
          } else {
            wx.setStorageSync('user', ress.data[0]);
          }
        })
      },
      fail(res) {
        console.log('上传失败', res)
      }
    })
},

	location(e){//定位功能
		var that = this;
		wx.getLocation({
			type: "wgs84",
			success(res) {
					wx.chooseLocation({
							latitude: res.latitude,
							longitude: res.longitude,
							success: function(data){
								console.log(data)
								//data : address name
								that.setData({
									address:data.name
								})
							}
					})
			}
		})
	},

});

